package cn.tedu.basic.loop;/**
 * @Author 金帮嘉诚 @Date 2021-07-24 星期六 10:05
 */
/*本类用于测试*/

/**
 * 知识点
 */
public class YangHuiTriangle {
    //用于补齐数据长度的单个字符串
    private static String singleString = " ";

    public static void main(String[] args) {
        printYangHuiTriangle(5);
    }

    public static long[][] ConstructionYangHuiTriangle(int line) {
        long[][] YangHui = new long[line][];
        for (int i = 0; i < YangHui.length; i++) {
            //初始化杨辉三角数组
            YangHui[i] = new long[i + 1];
            //添加每行首尾元素 1
            YangHui[i][0] = YangHui[i][i] = 1;
            //添加每行除首尾以外的元素
            for (int j = 1; j <= i - 1; j++) {
                YangHui[i][j] = YangHui[i - 1][j - 1] + YangHui[i - 1][j];
            }
        }
        return YangHui;
    }

    public static String[][] adjustArray(long[][] YangHui, String singleString) {
        //用于记录单个最长数据的长度
        int maxSingleDataLength = 0;
        for (long[] ints : YangHui) {
            for (long anInt : ints) {
                //将int类型的元素转换为字符串 为了方便测量其数据的长度
                String data = String.valueOf(anInt);
                //与记录数据最大长度的变量进行比较，如果大于最大长度的变量，将其值进行替换
                if (data.length() > maxSingleDataLength) {
                    maxSingleDataLength = data.length();
                }
            }
        }


        //开始补齐数据长度，使每个数据长度相同
        // ​首先新建一个数组 因为补齐数据需要用到字符串进行追加，所以创建一个字符串数组
        String[][] newArray = new String[YangHui.length][YangHui.length];
        for (int i = 0; i < YangHui.length; i++) {
            for (int j = 0; j < YangHui[i].length; j++) {
                //用来记录要补齐的长度
                int length;
                //将数据与最大数据长度的变量进行比较 来获取要补齐的长度
                //需要先将数据由int类型转为String类型--->String.valueOf(YangHui[i][j])
                length = maxSingleDataLength - String.valueOf(YangHui[i][j]).length();
                //将数据变为字符串类型，来进行追加长度StringBuilder的构造方法只能传入字符串
                StringBuilder stringBuilder = new StringBuilder(String.valueOf(YangHui[i][j]));
                for (int k = 0; k < length; k++) {
                    stringBuilder.append(singleString);

                }
                //将追加后的数据由可变字符序列变为不可变字符序列 即 StringBulider-->String
                //并将其保存到新的数组中
                newArray[i][j] = stringBuilder.toString();
            }
        }
        return newArray;
    }

    public static void printYangHuiTriangle(int line) {
        //先构建一个杨辉三角
        long[][] longs = ConstructionYangHuiTriangle(line);
        //将创建出来的杨辉三角进行打印前的调整
        String[][] newArray = adjustArray(longs, singleString);
        //打印显示要居中 将每个数据之间的一个空格用多个字符来代替 追加singleString
        //也就是获取单个数据字符串长度 作为单个需要打印补齐的空格长度
        int length1 = newArray[0][0].length() - singleString.length();
        if (length1 > 0) {
            StringBuilder stringBuilder1;
            stringBuilder1 = new StringBuilder(singleString);
            for (int i = 0; i < length1; i++) {
                stringBuilder1.append(singleString);
            }
            singleString = stringBuilder1.toString();
        }
            //循环遍历
            for (int i = 0; i < newArray.length; i++) {
                for (int j = 0; j < newArray.length - i - 1; j++) {
                    System.out.print(singleString);
                }
                for (int j = 0; j <= i; j++) {
                    //打印每个字符数据+singleString 最后一个数据不加
                    if (j != i) {
                        System.out.print(newArray[i][j] + singleString);
                    } else {
                        //打印完最后一个数据进行换行
                        System.out.println(newArray[i][j]);
                    }
                }
            }

    }
}
